package com.jin.webvideo.base.rpc;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;

public class RpcRegister implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
        AnnotationAttributes attr = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableRpc.class.getName()));

        ClassPathClientScanner scanner = new ClassPathClientScanner(registry);
        scanner.addIncludeFilter(new AnnotationTypeFilter(RpcInterface.class));
        scanner.setFactoryBeanClass(RpcFactoryBean.class);
        scanner.setHttpClientBean(attr.getString("httpClient"));
        scanner.setEnvironmentBeanName(attr.getString("environment"));
        scanner.scan(attr.getStringArray("basePackage"));
        ImportBeanDefinitionRegistrar.super.registerBeanDefinitions(importingClassMetadata, registry, importBeanNameGenerator);
    }
}
